from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.kq_user_do import KhUser
from module_admin.entity.vo.kq_user_vo import Kq_userPageQueryModel, Kq_userModel
from utils.page_util import PageUtil


class Kq_userDao:
    """
    考勤用户花名册模块数据库操作层
    """

    @classmethod
    async def get_kq_user_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据序号获取考勤用户花名册详细信息

        :param db: orm对象
        :param id: 序号
        :return: 考勤用户花名册信息对象
        """
        kq_user_info = (
            (
                await db.execute(
                    select(KhUser)
                    .where(
                        KhUser.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return kq_user_info

    @classmethod
    async def get_kq_user_detail_by_info(cls, db: AsyncSession, kq_user: Kq_userModel):
        """
        根据考勤用户花名册参数获取考勤用户花名册信息

        :param db: orm对象
        :param kq_user: 考勤用户花名册参数对象
        :return: 考勤用户花名册信息对象
        """
        kq_user_info = (
            (
                await db.execute(
                    select(KhUser).where(
                        KhUser.user_id == kq_user.user_id if kq_user.user_id else True,
                        KhUser.user_name == kq_user.user_name if kq_user.user_name else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return kq_user_info

    @classmethod
    async def get_kq_user_list(cls, db: AsyncSession, query_object: Kq_userPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取考勤用户花名册列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 考勤用户花名册列表信息对象
        """
        query = (
            select(KhUser)
            .where(
                KhUser.user_id == query_object.user_id if query_object.user_id else True,
                KhUser.user_name.like(f'%{query_object.user_name}%') if query_object.user_name else True,
                KhUser.phone_number == query_object.phone_number if query_object.phone_number else True,
                KhUser.department == query_object.department if query_object.department else True,
                KhUser.person_id == query_object.person_id if query_object.person_id else True,
                KhUser.employment_date == query_object.employment_date if query_object.employment_date else True,
            )
            .order_by(KhUser.id)
            .distinct()
        )
        kq_user_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return kq_user_list

    @classmethod
    async def add_kq_user_dao(cls, db: AsyncSession, kq_user: Kq_userModel):
        """
        新增考勤用户花名册数据库操作

        :param db: orm对象
        :param kq_user: 考勤用户花名册对象
        :return:
        """
        db_kq_user = KhUser(**kq_user.model_dump(exclude={}))
        db.add(db_kq_user)
        await db.flush()

        return db_kq_user

    @classmethod
    async def edit_kq_user_dao(cls, db: AsyncSession, kq_user: dict):
        """
        编辑考勤用户花名册数据库操作

        :param db: orm对象
        :param kq_user: 需要更新的考勤用户花名册字典
        :return:
        """
        await db.execute(update(KhUser), [kq_user])

    @classmethod
    async def delete_kq_user_dao(cls, db: AsyncSession, kq_user: Kq_userModel):
        """
        删除考勤用户花名册数据库操作

        :param db: orm对象
        :param kq_user: 考勤用户花名册对象
        :return:
        """
        await db.execute(delete(KhUser).where(KhUser.id.in_([kq_user.id])))

